package com.freshcity.page.tree
{
	import com.freshcity.component.container.Component;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.tree.WaterItem;
	import com.maccherone.json.JSON;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.GlowFilter;
	import flash.utils.Dictionary;
	
	public class WaterBar extends Component
	{
		private var _data:Array;
		private var _itemDict:Dictionary=new Dictionary();
		private var _selectedItem:WaterItem;
		private var _hasWater:Boolean;
		
		public function WaterBar(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			this.mouseEnabled = false;
			super(parent, xpos, ypos);
		}
		
		public function setData(value:String):void
		{
			_hasWater = false;
			for each (var j:WaterItem in _itemDict) 
			{
				delete _itemDict[j.index];
				removeChild(j);
				j.removeEventListener(MouseEvent.CLICK,onMouseClick);
			}			
			
			_data = JSON.decode(value);;
			var it:WaterItem;
			var xpos:int;
			var i:int;
			
			for each (var cat:int in _data) 
			{
				it = new WaterItem(i,cat,xpos);
				_itemDict[i] = it;
				addChild(it);
				if(cat>0){
					_hasWater = true;
					it.addEventListener(MouseEvent.CLICK,onMouseClick);
				}
				xpos += 15;
				i++;
			}
		}
		
		protected function onMouseClick(event:MouseEvent):void
		{
			if(_selectedItem){
				_selectedItem.mark=false;
			}
			_selectedItem = event.currentTarget as WaterItem;
			_selectedItem.mark=true;
			this.dispatchEvent(new DataEvent(Event.SELECT,_selectedItem));
		}
		
		public function hasWater():Boolean
		{
			return _hasWater;
		}
		
		override public function destroy():void
		{
			super.destroy();
		}

		public function get selectedItem():WaterItem
		{
			return _selectedItem;
		}

		public function set selectedItem(value:WaterItem):void
		{
			_selectedItem = value;
		}
		
		override public function show(event:Event=null):void
		{
		}
	}
}